﻿// -----------------------------------------------------------------------
// <copyright file="Utility.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace HashManager
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using HashMaster.Lib;
    using System.Net;
    using System.Runtime.Serialization;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Utility
    {
        private static WebResponse ResponseBuilder(string requestUrl, string method)
        {
            WebRequest request = WebRequest.Create(requestUrl);
            request.Method = method;
            if (method == "PUT")
            {
                request.ContentLength = 0;
            }
            if (method=="POST")
            {
                request.ContentLength = 0;
            }
            WebResponse response = request.GetResponse();
            return response;
        }

        internal static List<Task> GetAllTasksDetail(string serviceUrl)
        {
            string requestUrl = string.Format("{0}task/?mode=2", serviceUrl);

            DataContractSerializer ds = new DataContractSerializer(typeof(List<Task>));
            List<Task> ret = (List<Task>)ds.ReadObject(ResponseBuilder(requestUrl, "GET").GetResponseStream());

            return ret;

        }

        public static Boolean AddNewTask(string imei, string sha, int sliceWidth, string serviceUrl)
        {
            string requestUrl = string.Format("{0}task/{1}/{2}/{3}", serviceUrl, imei, sha, sliceWidth.ToString());

            DataContractSerializer ds = new DataContractSerializer(typeof(Boolean));
            Boolean ret = (Boolean)ds.ReadObject(ResponseBuilder(requestUrl, "POST").GetResponseStream());

            return ret;
        }

        internal static Boolean ModifyTask(string imei, int priority, string memo, string serviceUrl) 
        {
            if (memo == string.Empty) memo = "Empty";

            string requestUrl = string.Format("{0}task/{1}/{2}/{3}", serviceUrl, priority.ToString(), imei, memo);

            DataContractSerializer ds = new DataContractSerializer(typeof(Boolean));
            Boolean ret = (Boolean)ds.ReadObject(ResponseBuilder(requestUrl, "PUT").GetResponseStream());

            return ret;
        }
    }
}
